package com.chzc.fm.duotin.ui.channel;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.adapter.recyclerview.TypeAnchorAdapter;
import com.chzc.fm.duotin.bean.channel.AnchorSpecialItem;
import com.chzc.fm.duotin.utils.DuotinTask;
import com.chzc.fm.duotin.utils.GlobalParam;
import com.chzc.fm.duotin.utils.HttpUtil;
import com.chzc.fm.duotin.utils.LogUtil;
import com.chzc.fm.duotin.widget.TitleBar;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Administrator on 2016/7/5.
 */
public class TypeAnchorActivity extends AppCompatActivity implements View.OnClickListener {

    private TitleBar titleBar;
    private TextView tvall;
    private RecyclerView recyclerView;

    private TypeAnchorAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_typeanchor);

        initViews();

        initEvents();

        initDatas();
    }

    public void initViews()
    {
        titleBar = (TitleBar) findViewById(R.id.typeanchor_titlebar);
        tvall = (TextView) findViewById(R.id.typeanchor_all_tv);
        recyclerView = (RecyclerView) findViewById(R.id.typeanchor_content_rv);

        GridLayoutManager manager = new GridLayoutManager(TypeAnchorActivity.this,4);
        recyclerView.setLayoutManager(manager);
    }

    public void initEvents()
    {
        tvall.setOnClickListener(this);
    }

    public void initDatas()
    {
        titleBar.setLeft(true,R.drawable.arrow_back_white);
        titleBar.setRight(true);
        titleBar.setSearchBarVisibilty(false);
        titleBar.setTitleBarVisibility(true,"主播电台");
        titleBar.setLeftListener(new TitleBar.mTitleBarListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        getTypeAnchor(new DuotinTask.IRequestCallback() {
            @Override
            public void success(Object object) {

                try {
                    JSONObject root = new JSONObject(object.toString());
                    String error_msg = root.getString("error_msg");
                    if ("SUCCESS".equals(error_msg))
                    {
                        JSONArray datalist = root.getJSONArray("data");
                        final List<AnchorSpecialItem> itemdatalist = AnchorSpecialItem.arrayAnchorItemFromData(datalist.toString());

                        LogUtil.w("datalist>>>" + datalist);

                        adapter = new TypeAnchorAdapter(TypeAnchorActivity.this,itemdatalist);
                        recyclerView.setAdapter(adapter);

                        adapter.setItemOnClickListener(new TypeAnchorAdapter.mItemOnClickListener() {
                            @Override
                            public void OnItemClick(int position) {
                                switch (position)
                                {
                                    default:
                                        Intent intent = new Intent(TypeAnchorActivity.this,HostStationActivity.class);
                                        Bundle bundle = new Bundle();
                                        bundle.putSerializable("specialitem",itemdatalist.get(position));
                                        intent.putExtras(bundle);
                                        startActivity(intent);
                                }
                            }
                        });
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void error(String msg) {

            }
        });
    }


    public void getTypeAnchor(DuotinTask.IRequestCallback callback)
    {
        DuotinTask.IRequest request = new DuotinTask.IRequest() {
            @Override
            public Object doRequest() {
                return HttpUtil.doGet(GlobalParam.URL_TYPEANCHOR);
            }
        };

        new DuotinTask(request,callback).execute();
    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.typeanchor_all_tv:
                tvall.setSelected(true);
                finish();
                break;

        }
    }
}
